package cn.nawang.ebeim.api.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtils
 * @Description: TODO
 * @author 许炜毅
 * @date 2015年8月5日 下午2:14:47
 * @version v1.0
 */
public class DateUtils {
  
  public static String YMDPARTTERN="yyyy-MM-dd HH:mm:ss";

  /**
   * 取得某个月的天数 d
   * 
   * @param year int 指定哪一年
   * @param month int 指定哪一个月
   */
  public static int getDaysOfMonth(int year, int month) {
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.set(Calendar.YEAR, year);
    calendar2.set(Calendar.YEAR, year);
    calendar1.set(Calendar.MONTH, month - 1);
    calendar2.set(Calendar.MONTH, month);
    int days = calendar2.get(Calendar.DAY_OF_YEAR) - calendar1.get(Calendar.DAY_OF_YEAR);
    return days;
  }

  /**
   * 判断时间date1是否在时间date2之前 时间格式 2005-4-21 16:16:34
   * 
   * @param date1
   * @param date2
   * @return
   */
  public static boolean isDateBefore(String date1, String date2) {
    try {
      DateFormat df = DateFormat.getDateTimeInstance();
      return df.parse(date1).before(df.parse(date2));
    } catch (Exception e) {
      return false;
    }
  }

  /**
   * 判断当前时间是否在时间date2之前 时间格式 2005-4-21 16:16:34
   * 
   * @param date2
   * @return
   */
  public static boolean isDateBefore(String date2) {
    try {
      Date date1 = new Date();
      DateFormat df = DateFormat.getDateTimeInstance();
      return date1.before(df.parse(date2));
    } catch (Exception e) {
      return false;
    }
  }

  /**
   * 得到几小时前、后的日期
   * 
   * @return
   */
  public static Date getDateTimeAfter(int hour) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(java.util.Calendar.HOUR, hour);
    return calendar.getTime();
  }

  /**
   * 得到几分钟前、后的日期
   * 
   * @return
   */
  public static Date getDateTimeMinuteAfter(int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(java.util.Calendar.MINUTE, minute);
    return calendar.getTime();
  }

  /**
   * 两个日期比较大小，格式：2009-01-03与2009-01-04
   * 
   * @param startDate 第一个日期，字符串
   * @param endDate 第二个日期，字符串
   * @return 如果开始日期大于结束日期，返回false
   */
  public static boolean dateCompareTo(String startDate, String endDate) {
    if (startDate.compareTo(endDate) > 0) {
      return false;
    } else {
      return true;
    }
  }

  /**
   * 得到几天后的时间(几天前的时间用负数)
   * 
   * @param d
   * @param day (几天前的时间用负数)
   * @return
   * @throws ParseException
   */
  public static Date getDateAfter(Date d, int addDayCount) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);
    now.set(Calendar.DATE, now.get(Calendar.DATE) + addDayCount);
    return now.getTime();
  }

  /**
   * 得到几个月后的时间(几个月前的时间用负数)
   * 
   * @param d
   * @param day (几个月前的时间用负数)
   * @return
   * @throws ParseException
   */
  public static Date getDateAfterMonth(Date d, int addMonthCount) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);
    now.add(Calendar.MONTH, addMonthCount);
    return now.getTime();
  }

  /**
   * 两个日期比较大小，格式：2009-01-03与2009-01-04
   * 
   * @param startDate 第一个日期，字符串
   * @param endDate 第二个日期，字符串
   * @return 如果开始日期大于结束日期，返回true
   */
  public static boolean dateCompareTo2(String startDate, String endDate) {
    if (startDate.compareTo(endDate) >= 0) {
      return true;
    } else {
      return false;
    }
  }

  /**
   * 将日期型转换成12位长字符串，格式为"YYYYMMDDHHMI"
   * 
   * @param d
   * @return
   */
  public static String toStringLong(Date d) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    return sdf.format(d);
  }

  /**
   * 接受YYYY-MM-DD的日期字符串参数,返回两个日期相差的天数
   * 
   * @param start
   * @param end
   * @return
   * @throws ParseException
   */
  public static long getDistDates(String start, String end) {
    Date startDate = null;
    Date endDate = null;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      startDate = sdf.parse(start);
      endDate = sdf.parse(end);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return getDistDates(startDate, endDate);
  }

  /**
   * 返回两个日期相差的天数
   * 
   * @param startDate
   * @param endDate
   * @return
   * @throws ParseException
   */
  public static long getDistDates(Date startDate, Date endDate) {
    long totalDate = 0;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);
    long timestart = calendar.getTimeInMillis();
    calendar.setTime(endDate);
    long timeend = calendar.getTimeInMillis();
    totalDate = Math.abs((timeend - timestart)) / (1000 * 60 * 60 * 24);
    return totalDate;
  }

  /**
   * @author fanyb
   * @Description: 获取当天的前后几天的起始时间
   * @param day 天数
   * @return 当天的前后几天的起始时间戳
   * @date Aug 6, 20153:23:45 PM
   *
   * @修改人
   * @修改日期
   */
  public static Long getTodayStartTime(int day) {
    Calendar todayStart = Calendar.getInstance();
    todayStart.add(Calendar.DAY_OF_MONTH, day);
    todayStart.set(Calendar.HOUR_OF_DAY, 0);
    todayStart.set(Calendar.MINUTE, 0);
    todayStart.set(Calendar.SECOND, 0);
    todayStart.set(Calendar.MILLISECOND, 0);
    return todayStart.getTime().getTime();
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取当天的前后几天的结束时间
   * @parame day 天数
   * @return Long 获取当天的前后几天的结束时间戳
   * @date Aug 6, 20153:24:41 PM
   *
   * @修改人
   * @修改日期
   */
  public static Long getTodayEndTime(int day) {
    Calendar todayEnd = Calendar.getInstance();
    todayEnd.add(Calendar.DAY_OF_MONTH, day);
    todayEnd.set(Calendar.HOUR_OF_DAY, 23);
    todayEnd.set(Calendar.MINUTE, 59);
    todayEnd.set(Calendar.SECOND, 59);
    todayEnd.set(Calendar.MILLISECOND, 999);
    return todayEnd.getTime().getTime();
  }

  /**
   * @author fanyb
   * @Description: 获取当前月前后几个月起始时间
   * @param month 月份
   * @return 当前月前后几个月的起始时间戳
   * @date Aug 6, 20153:23:45 PM
   *
   * @修改人
   * @修改日期
   */
  public static Long getToMonthStartTime(int month) {
    Calendar todayStart = Calendar.getInstance();
    todayStart.add(Calendar.MONTH, month);
    todayStart.set(Calendar.DATE, 1);
    todayStart.set(Calendar.HOUR_OF_DAY, 0);
    todayStart.set(Calendar.MINUTE, 0);
    todayStart.set(Calendar.SECOND, 0);
    todayStart.set(Calendar.MILLISECOND, 0);
    return todayStart.getTime().getTime();
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取当前月前后几个月的结束时间
   * @param month 月份
   * @return Long 获取当前月前后几个月的结束时间戳
   * @date Aug 6, 20153:24:41 PM
   *
   * @修改人
   * @修改日期
   */
  public static Long getToMonthEndTime(int month) {
    Calendar todayEnd = Calendar.getInstance();
    todayEnd.add(Calendar.MONTH, month + 1);
    todayEnd.set(Calendar.DATE, 0);
    todayEnd.set(Calendar.HOUR_OF_DAY, 23);
    todayEnd.set(Calendar.MINUTE, 59);
    todayEnd.set(Calendar.SECOND, 59);
    todayEnd.set(Calendar.MILLISECOND, 999);
    return todayEnd.getTime().getTime();
  }
  
  public static Long getDateEndTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return calendar.getTime().getTime();
  }

  /**
   * 
   * @author fanyb
   * @Description: 几年以后的时间长整形值
   * @param year 几年
   * @return long 整形值
   * @date Aug 25, 20153:09:31 PM
   *
   * @修改人
   * @修改日期
   */
  public static long getYearAfterLong(int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.YEAR, year);
    return calendar.getTime().getTime();
  }

  private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
    return new SimpleDateFormat(parttern);
  }

  public static String date2String(Date date, String parttern) {
    String dateString = null;
    if (date != null) {
      try {
        dateString = getDateFormat(parttern).format(date);
      } catch (Exception e) {}
    }
    return dateString;
  }

  public static String date2String(String parttern) {
    return date2String(new Date(), parttern);
  }

  public static String date2StringYMD() {
    return date2String("yyyyMMdd");
  }

  public static String date2StringYMDHM() {
    return date2String("yyyyMMdd HH:mm");
  }

  public static String date2StringYMDHMS() {
    return date2String("yyyy-MM-dd HH:mm:ss");
  }

  public static String date2StringHM() {
    return date2String("HH:mm");
  }

  public static long currentTimeMillis() {
    return System.currentTimeMillis();
  }
  
  public static String longTime2String(long time){
    Date date=new Date(time);
    String dateStr=date2String(date,YMDPARTTERN);
    return dateStr;
  }
}
